/**
 * Created by DevilsEyes on 2016/2/13.
 * ==========================
 *      单例 - 统计数据 stats
 * ==========================
 * 用于存放所有统计数据
 *
 *****************************/
'use strict';
var db = require('../unit/db.js').mongoose;
var $doc = require('./doc.js');
var Schema = db.Schema;

var stats_Schema = new Schema({

    id: String,

    total: Number,//总数
    success: Number,//已匹配
    wait: Number,//待匹配
    last: String,//上一次匹配结果的root + deep
    max: Number,//目前的最大匹配值

    status: Number
    //状态
    // 0,闲置
    // 1,运行中
    // 2,完成

    //create_date: {
    //    type: Date,
    //    default: Date.now
    //}
});

var stats_Model = db.model("stats", stats_Schema);
var id = 'only';

module.exports = {

    creat: (obj, callback) => {
        obj.id = id;
    },

    get: (callback) => {
        stats_Model.findOne({id: id}, (err, doc) => {
            if (!err) {
                if (doc) {
                    callback(err, doc);
                } else {
                    var stats_Entity = new stats_Model({
                        id: id,
                        total: 0,
                        success: 0,
                        max: 0,
                        wait: 0,
                        last: '',
                        status: 0
                    });
                    stats_Entity.save((err, stats) => {
                        callback(err, stats);
                    });
                }
            } else {
                //todo 错误种类的判断
            }
        });
    },

    count: (last, callback) => {
        var stats_Entity = {
            total: null,
            success: null,
            max: null,
            wait: null,
            status: null
        };
        if (last)stats_Entity['last'] = last;
        var updated = (err) => {
            var o = stats_Entity;
            if (o.total === null || o.success === null || o.max === null || o.wait === null)return;
            if (o.total == o.success && o.success > 0) {
                //全部完成
                o.status = 2;
            } else if (o.success == 0 && o.wait === 0) {
                //无成功项且无待匹配项
                o.status = 0;
            } else {
                //其他
                o.status = 1;
            }
            stats_Model.find({id: id}).update(stats_Entity, (err, res) => {
                //noop
            });

            callback(err, stats_Entity);
        };

        //统计未匹配数量
        $doc.$Model
            .find({status: 0})
            .count((err, count) => {
                stats_Entity.wait = count;
                updated(err);
            });

        //统计已匹配数量
        $doc.$Model
            .where('status')
            .gt(0)
            .count((err, count) => {
                stats_Entity.success = count;
                updated(err);
            });

        //统计总数
        $doc.$Model
            .find()
            .count((err, count) => {
                stats_Entity.total = count;
                updated(err);
            });

        //统计最大匹配度
        $doc.$Model
            .find()
            .sort({status: 'desc'})
            .findOne((err, doc) => {
                if (doc === null) {
                    stats_Entity.max = -3;
                } else {
                    stats_Entity.max = doc.status;
                }
                updated(err);
            })
    },

    clear: (callback) => {
        stats_Model.remove({}, (err, stats) => {
            //noop
        })
    }

};